#include "../header.h"
//文件锁
int main(int argc, char const *argv[])
{
    struct flock lock = {0};
    lock.l_type = F_WRLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len = 0;
    lock.l_pid = getpid();

    int fd = open("aha.txt",O_RDWR|O_TRUNC);
    while(fcntl(fd,F_SETLK,&lock) )
    {
        printf("文件锁设定失败\n");
        sleep(1);
    }
    printf("文件锁设定成功\n");
    char *str = "hello world\n";
    while(1)
    {
        write(fd,str,strlen(str));
        sleep(1);
    }
    lock.l_type = F_UNLCK;
    if (!fcntl(fd,F_SETLK,&lock.l_type))
    {
      printf("解锁成功\n");  
    }
    else
    printf("解锁失败\n");
    close(fd);
    
    return 0;
}
